var CommonTemplateUtil = {
  getTableTemplateConfig: function (templateType) {
    var columnNames = [];
    switch (templateType) {
      case 'purchaseOrder':
        columnNames = ['orderPurchaseColumns', 'warehousePurchaseColumns'];
        break;
      case 'prePurchaseOrder':
        columnNames = ['prePurchaseColumns'];
        break;
      case 'warehouseInoutOrder':
        columnNames = ['warehouseInoutOrderColumns', 'productWarehouseInoutOrderColumns'];
        break;
      case 'warehouseInOrder':
        columnNames = ['warehouseInOrderColumns','productWarehouseInOrderColumns'];
        break;
      case 'outOrder':
        columnNames = ['outOrderColumns'];
        break;
      case 'issueOrder':
        columnNames = ['issueOrderColumns'];
        break;
      case 'returnOrder':
        columnNames = ['returnOrderColumns'];
        break;
    }
    return new Promise(function (resole, reject) {
      var name = i18n.getMsg(`templateSetting.type.${templateType}`);
      ajaxRequest('printTemplate/getCommonOrderTemplates', {
        type: templateType,
        name: name
      }).then(function (templates) {
        var template = templates[0];
        var templateConfig;
        if (template === undefined || SysUtil.isEmpty(template.template)) {
          templateConfig = new TemplateConfig(templateType);
        } else {
          var config = JSON.parse(template.template);
          templateConfig = new TemplateConfig(templateType, config);
        }

        for (var i = 0; i < columnNames.length; i++) {
          var columnName = columnNames[i];
          if (templateConfig[columnName] !== undefined) {
            continue;
          }
          var column = TemplateColumnConfigUtil.getDefaultTemplateColumn(columnName);
          templateConfig.initColumns(column, columnName);
        }

        resole(templateConfig);
      }).catch(function (error) {
        reject(error);
      });
    });
  },

  formatUnit2px: function (value, unit) {
    if (unit === 'px') {
      return value;
    }

    var $el = $('<div class="cal-width-div"></div>');
    $el.css({
      width: value + unit
    });
    $('#app').append($el);
    value = $el.width();
    $el.remove();
    value = Math.floor(value);
    return value;
  },

  getPageSize: function (pageSize) {
    var width, height;
    var direction = pageSize.direction;
    if (direction === 'vertical') {
      width = pageSize.width;
      height = pageSize.height;
    } else {
      width = pageSize.height;
      height = pageSize.width;
    }

    var unit = pageSize.unit;
    if (unit !== 'px') {
      width = this.formatUnit2px(width, unit);
      height = this.formatUnit2px(height, unit);
    }
    return {
      width: width,
      height: height
    };
  },

  setColumnConfig: function (columns, columnName) {
    var columnConfig = TemplateColumnConfigUtil.getTemplateColumn(columnName, true);
    for (var i = 0; i < columns.length; i++) {
      var column = columns[i];
      column.config = columnConfig[column.field];
      if (column.hasSort) {
        column.sortType = '';
      }
    }
  }
};
